"use strict";
/**
 * @name role
 * 查询其他玩家信息
 */



/**
 * @name ladder
 * @param string uid 玩家唯一标识,多个以","分割
 * @param int lineupId 编组id
 * 对手编组详情
 * @tip 包含数据 fs:编组战力，hero:[{id,lv,slv,blv}...]武将信息 equip:[{}]
 */
exports.ladder = async function () {
    let uid = this.get('uid', 'string');
    if (!uid) {
        return this.error('error', 'uid empty');
    }
    let arr_uid = uid.split(',').filter(u => u.length > 0);
    if (arr_uid.length < 1) {
        return this.error("uid empty")
    }

    const lineupId = this.get('lineupId', 'int') || 11;

    let fk = ["lineupSet." + lineupId];
    let role = this.model.mongo('role', this.sid);
    let item = this.model.mongo('item', this.sid);
    let ret = await role.get(arr_uid, fk);
    let tempList = [];
    let equipDic = {}


    for (let r in ret) {
        equipDic[r] = {};
        if (ret[r].lineupSet) {
            if (ret[r].lineupSet[lineupId]) {
                if (!ret[r].lineupSet[lineupId].fs) {
                    ret[r].lineupSet[lineupId].fs = 0;
                }
                tempList = tempList.concat(ret[r].lineupSet[lineupId].heroColl);

                for (let k = 0; k < ret[r].lineupSet[lineupId].equipColl.length; ++k) {
                    let ee = ret[r].lineupSet[lineupId].equipColl[k];
                    if (ee) {
                        equipDic[r][ee] = k + 1;
                    }
                }
                tempList = tempList.concat(ret[r].lineupSet[lineupId].equipColl);
            }
        }
    }
    // console.log(equipDic);
    //console.log(tempList);
    let option = {
        "multi": true,
        "projection": ["id", "lv", "slv", "blv", "rlv"]
    };
    let cardRet = await item.find({
        '_id': {
            '$in': tempList
        }
    }, option);




    for (let r in ret) {
        if (ret[r].lineupSet) {
            if (ret[r].lineupSet[lineupId]) {
                ret[r].fs = ret[r].lineupSet[lineupId].fs;
                ret[r].hero = [];
                ret[r].equip = {};
                for (let h of ret[r].lineupSet[lineupId].heroColl) {
                    if (cardRet[h]) {
                        ret[r].hero.push(cardRet[h]);
                    }
                }
                for (let h of ret[r].lineupSet[lineupId].equipColl) {
                    if (cardRet[h] && equipDic[r][h]) {
                        ret[r].equip[equipDic[r][h] - 1] = cardRet[h];
                    }
                }
                // console.log(ret[r].equip);
            }
            delete ret[r].lineupSet;

        }

    }
    // console.log(ret);
    return ret;
};



/**
 * @name simple
 * @param string uid 玩家唯一标识,多个以","分割
 * @param string key 额外字段,多个以","分割
 * 玩家简单列表
 */
exports.simple = function () {
    let uid = this.get('uid');
    let key = this.get('key', 'string') || '';
    if (!uid) {
        return this.error('error', 'uid empty');
    }
    let arr_uid = uid.split(',').filter(u => u.length > 0);
    if (arr_uid.length < 1) {
        return this.error("uid empty")
    }

    // let fk = ['_id', 'lv', 'icon', "name"];
    let fk = ['_id'];

    let arr_key = key.split(",");
    for (let k of arr_key) {
        if (k) fk.push(k);
    }

    let role = this.model.mongo('role', this.sid);
    return role.get(arr_uid, fk);
};


/**
 * @name item
 * @param string uid 要查询的玩家的uid
 * @param int bag 背包ID，留空获取所有
 * 查询单个玩家道具列表
 */
exports.item = function () {
    const uid = this.get('uid', 'string');
    const bag = this.get("bag", "int");
    if (!uid) {
        return this.quick_error('M2NCRGR1', 'uid为空');
    }
    let model = this.model.mongo("item", this.sid, uid);
    return model.mget(bag);
}


/**
 * @name itemM
 * @param string uids [玩家uid]
 * @param int bag 背包ID（可选）
 * @tip 消耗很大，慎重调用，强烈建议填写背包类型
 * 查询多个玩家道具列表 （慎重）
 */
exports.itemM = function () {
    const uids = this.get('uids', 'string');
    const bag = this.get("bag", "int");
    if (!uids) {
        return this.quick_error('M2NCRGR2', 'uids为空');
    }
    let arr_uids = uids.split(',').filter(u => u.length > 0);
    if (arr_uids.length > 20) {
        return this.quick_error('M2NCRGR3', '一次性查询玩家太多，你是不是想捣乱');
    }
    let result = [];
    let promise_list = [];
    for (let i of arr_uids) {
        promise_list.push(this.model.mongo("item", this.sid, i).mget(bag));
    }
    return Promise.all(promise_list);
}






/**
 * @name getUnitandChip
 * @param string unitList 单位的唯一标识,多个以","分割
 * 根据单位获取战斗相关的信息
 */
exports.getUnitandChip = async function () {
    let unitList = this.get('unitList', 'string');
    if (!unitList) {
        return this.error('error', 'uid empty');
    }
    let arr_unit = unitList.split(',').filter(u => u.length > 0);
    if (arr_unit.length < 1) {
        return this.error("uid empty")
    }


    // let uidData = this.get('uids','json');
    // if( !uidData ){
    //     return this.error('error', 'uid empty');
    // }


    let item = this.model.mongo('item', this.sid);
    // let unitList = [];
    // for(let v in uidData){
    //     unitList = unitList.concat(uidData[v]);
    // }
    let option = {
        "multi": true,
        "projection": ["id", "lv", "slv", "rlv", "skillList", "chipList"]
    };
    let unitRetList = await item.find({
        '_id': {
            '$in': arr_unit
        }
    }, option);



    let arr_chip = [];
    for (let v in unitRetList) {
        for (let k in unitRetList[v].chipList) {
            arr_chip.push(unitRetList[v].chipList[k]);
        }
    }
    let chipOption = {
        "multi": true,
        "projection": ["id", "lv", "mainAttr", "otherAttr"]
    };
    let chipRetList = await item.find({
        '_id': {
            '$in': arr_chip
        }
    }, chipOption);


    return { "unitList": unitRetList, "chipList": chipRetList };
    // let arr_uid = uid.split(',').filter(u => u.length > 0);
    // if (arr_uid.length < 1) {
    //     return this.error("uid empty")
    // }

    // const lineupId = this.get('lineupId', 'int') || 11;

    // let fk = ["lineupSet." + lineupId];
    // let role = this.model.mongo('role', this.sid);
    // let item = this.model.mongo('item', this.sid);
    // let ret = await role.get(arr_uid, fk);
    // let tempList = [];
    // let equipDic={}


    // for (let r in ret) {
    //     equipDic[r]={};
    //     if (ret[r].lineupSet) {
    //         if (ret[r].lineupSet[lineupId]) {
    //             if (!ret[r].lineupSet[lineupId].fs) {
    //                 ret[r].lineupSet[lineupId].fs = 0;
    //             }
    //             tempList = tempList.concat(ret[r].lineupSet[lineupId].heroColl);

    //             for(let k=0;k<ret[r].lineupSet[lineupId].equipColl.length;++k){
    //                 let ee=ret[r].lineupSet[lineupId].equipColl[k];
    //                 if(ee){
    //                      equipDic[r][ee]=k+1;
    //                 }
    //             }
    //             tempList = tempList.concat(ret[r].lineupSet[lineupId].equipColl);
    //         }
    //     }
    // }
    // // console.log(equipDic);
    // //console.log(tempList);
    // let option = {
    //     "multi": true,
    //     "projection": ["id", "lv", "slv", "blv","rlv"]
    // };
    // let cardRet = await item.find({
    //     '_id': {
    //         '$in': tempList
    //     }
    // }, option);




    // for (let r in ret) {
    //     if (ret[r].lineupSet) {
    //         if (ret[r].lineupSet[lineupId]) {
    //             ret[r].fs = ret[r].lineupSet[lineupId].fs;
    //             ret[r].hero = [];
    //             ret[r].equip={};
    //             for (let h of ret[r].lineupSet[lineupId].heroColl) {
    //                 if (cardRet[h]) {
    //                     ret[r].hero.push(cardRet[h]);
    //                 }
    //             }
    //             for (let h of ret[r].lineupSet[lineupId].equipColl) {
    //                 if (cardRet[h]&&equipDic[r][h]) {
    //                     ret[r].equip[equipDic[r][h]-1]=cardRet[h];
    //                 }
    //             }
    //             // console.log(ret[r].equip);
    //         }
    //         delete ret[r].lineupSet;

    //     }

    // }
    // console.log(ret);
    return ret;
};
